//
//  GSAdsView.m
//  01-图片轮播器
//
//  Created by Lemon on 15-05-10.
//  Copyright (c) 2015年 Lemon. All rights reserved.
//

#import "GSAdsView.h"
#import "UIImageView+WebCache.h"

@interface GSAdsView ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView * scroView;
@property (nonatomic,weak)  UIPageControl * pageControl;
@property (nonatomic,strong) NSTimer * timer;
@property (nonatomic,weak) UIImageView * leftView;
@property (nonatomic,weak) UIImageView * centerView;
@property (nonatomic,weak) UIImageView * rightView;
@property (nonatomic,assign) NSInteger leftIndex;
@property (nonatomic,assign) NSInteger rightIndex;
@property (nonatomic,assign) NSInteger centerIndex;
@end

@implementation GSAdsView

-(void)setCurrentPageTintColor:(UIColor *)currentPageTintColor
{
    _currentPageTintColor = currentPageTintColor;
    self.pageControl.currentPageIndicatorTintColor = currentPageTintColor;
    
}

-(void)setPageTintColor:(UIColor *)pageTintColor
{
    _pageTintColor = pageTintColor;
    self.pageControl.pageIndicatorTintColor = pageTintColor;
}

-(void)setPageControlCenter:(CGPoint)pageControlCenter
{
    _pageControlCenter = pageControlCenter;
    self.pageControl.center = self.pageControlCenter;
    
}

+(id)adsViewWithFrame:(CGRect)frame
{
  
    return  [[self alloc] initWithFrame:frame];
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _autoScroll = YES;
        UIScrollView * scroView = [[UIScrollView alloc] init];
        [self addSubview:scroView];
      
        
        scroView.backgroundColor = [UIColor lightGrayColor];
        
        scroView.pagingEnabled = YES;
        scroView.bounces = NO;
        
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewClick)];
        [scroView addGestureRecognizer:tgr];
        
        scroView.showsHorizontalScrollIndicator = NO;
        self.scroView = scroView;
        self.scroView.delegate = self;
        
        UIPageControl * pageControl = [[UIPageControl alloc] init];
        
        [self addSubview:pageControl];
        
        pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:1.00f];
        pageControl.pageIndicatorTintColor  =  [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.4];
        
        self.pageControl = pageControl;

       // [self startTime];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scroView.frame = self.bounds;
    self.centerView.frame = CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
    self.leftView.frame =CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.rightView.frame =CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);

    self.scroView.contentSize = CGSizeMake(3*self.frame.size.width, 0);
    self.scroView.contentOffset = CGPointMake(self.frame.size.width, 0);

    self.pageControl.center = CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.9);
}

/**
 * 设置一个图片位置及背景图
 *
 *  @param X         图片位置X值
 *  @param imageName 图片名称
 *
 *  @return 图片
 */
-(UIImageView *) imageViewWithCGPointX:(CGFloat)X backGroundImageNmae:(NSString *)imageName
{
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.backgroundColor = [UIColor whiteColor];
    

    if (self.contentMode) {
        imageView.contentMode = self.contentMode;
        imageView.clipsToBounds = YES;
    }
    
    [self.scroView addSubview:imageView];
    imageView.frame =CGRectMake(X, 0, self.frame.size.width, self.frame.size.height);

   [self setUpOneImageStateWithButton:imageView ImageName:imageName];

    
    return imageView;
}

/**
 *  设置一张图片状态
 *
 *  @param button
 *  @param imageName
 */
-(void) setUpOneImageStateWithButton:(UIImageView *)imageView ImageName:(NSString *)imageName
{
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageName]];
    
}

-(void) scrollViewClick
{
    if ([self.delegate respondsToSelector:@selector(gsAdsViewTouch:Index:)]) {
        
        [self.delegate gsAdsViewTouch:self Index:self.centerIndex];
    }
}

/**
 *  重写set方法
 *
 *  @param imagesArray
 */
-(void)setImagesArray:(NSArray *)imagesArray
{
  
    _imagesArray = imagesArray;
    
    [self.timer invalidate];
    self.timer = nil;
    _leftIndex =_imagesArray.count-1;
    _centerIndex = 0;
    _rightIndex = 1;

    self.pageControl.numberOfPages =_imagesArray.count;
    self.pageControl.currentPage = 0;
    [self startTime];
    if (self.autoScroll==NO) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    self.centerView = [self imageViewWithCGPointX:self.frame.size.width backGroundImageNmae:_imagesArray[0]];
    
    self.leftView = [self imageViewWithCGPointX:0 backGroundImageNmae:_imagesArray[_imagesArray.count-1]];
    
   
   
    if (imagesArray.count==1) {
         self.rightView = [self imageViewWithCGPointX:2*self.frame.size.width backGroundImageNmae:_imagesArray[0]];
            _pageControl.hidden = YES;
            _scroView.scrollEnabled = NO;
             [self.timer invalidate];
    }
    else{
        self.rightView = [self imageViewWithCGPointX:2*self.frame.size.width backGroundImageNmae:_imagesArray[1]];
            _pageControl.hidden = NO;
            _scroView.scrollEnabled = YES;

    }
    
  
}

-(void)setHiddenPageView:(BOOL)hiddenPageView
{
    
    _hiddenPageView = hiddenPageView;

    if (hiddenPageView) {
        
        self.pageControl.hidden = YES;
    }
}

-(void)setAutoScroll:(BOOL)autoScroll
{
    _autoScroll = autoScroll;
    
    if (autoScroll==YES) {
        [self startTime];
    }
    else
    {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void) startTime
{
    float time =self.intervalTime!=0?_intervalTime:3;
        NSTimer * timer = [NSTimer timerWithTimeInterval:time target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    self.timer = timer ;
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)stopTime
{
    [self.timer invalidate];
    self.timer =nil;
}

-(void)setIntervalTime:(float)intervalTime
{
    _intervalTime = intervalTime;
    [self.timer invalidate];
    [self startTime];
}

-(void) autoScroll
{
    if (self.imagesArray.count==1) {
        return;
    }
    [self.scroView setContentOffset:CGPointMake( 2 * self.frame.size.width, 0) animated:YES];
}

-(void)autoScrollWithEvent;
{
    if (self.imagesArray.count>1) {
        
        _centerIndex =_centerIndex  = (_centerIndex >= _imagesArray.count-1) ? 0 : _centerIndex+1;
        
        [self reloadViews];
        
        [UIView animateWithDuration:0.5 animations:^{
            self.scroView.contentOffset = CGPointMake( 2 * self.frame.size.width, 0);
        }];
        
        
    }
}



-(void)reloadViews
{
    //计算位置
    if (self.scroView.contentOffset.x ==2*self.frame.size.width) {
       _centerIndex  = (_centerIndex == _imagesArray.count-1) ? 0 : _centerIndex+1;
    }
    else if (self.scroView.contentOffset.x ==0)
    {
        _centerIndex = (_centerIndex == 0)? _imagesArray.count-1 : _centerIndex-1;
    }
     _leftIndex = (_centerIndex == 0) ? (_imagesArray.count-1) : (_centerIndex-1);
    _rightIndex = (_centerIndex == _imagesArray.count-1) ? 0 : (_centerIndex+1);
    
     //设置中间View
    [self setUpOneImageStateWithButton:self.centerView ImageName:_imagesArray[_centerIndex]];
    
    [self setUpOneImageStateWithButton:self.leftView ImageName:_imagesArray[_leftIndex]];
    
    [self setUpOneImageStateWithButton:self.rightView ImageName:_imagesArray[_rightIndex]];
    
    self.centerView.tag = _centerIndex;
   
    self.pageControl.currentPage = _centerIndex;
    
    if ([self.delegate respondsToSelector:@selector(gsAdsViewCurrentIndex:)]) {
        
        [self.delegate gsAdsViewCurrentIndex:_centerIndex];
    }
    
    self.scroView.contentOffset = CGPointMake(self.frame.size.width, 0);

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
    [self reloadViews];
   
}
//自动滚动setContentOffset完成后调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self reloadViews];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.autoScroll) {
        [self startTime];
    }
    
}

@end
